package com.demo.security.access.audit;

import java.util.Collection;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.security.core.Authentication;

import com.demo.security.access.AfterLogicInvocation;
import com.demo.security.access.LogicAttribute;
import com.demo.security.access.audit.prepost.PostAuditAttribute;
import com.demo.security.access.audit.prepost.PreAuditAttribute;
import com.demo.security.access.audit.prepost.PrePostInvocationAuditAdvice;

/**
 * 访问审计调用
 * 
 * @author ryuu.kk
 *
 */
public class AfterAuditInvocation implements AfterLogicInvocation<MethodInvocation> {

	private PrePostInvocationAuditAdvice advice;
	
	@Override
	public boolean supports(Class<?> clazz) {
		return MethodInvocation.class.isAssignableFrom(clazz);
	}

	@Override
	public boolean supports(LogicAttribute attribute) {
		return attribute instanceof PostAuditAttribute;
	}

	@Override
	public Object after(Authentication authentication, MethodInvocation mi, Collection<LogicAttribute> attributes, Object returnObject) {
		Object resultObj = returnObject;
		for (LogicAttribute attr : attributes) {
			if (supports(attr)) {
				System.out.println(authentication.getPrincipal() + " : " + returnObject);
				advice.after(authentication, mi, (PostAuditAttribute)attr, returnObject);
			}
		}
		
		return resultObj;
	}
	
	public void setAdvice(PrePostInvocationAuditAdvice advice) {
		this.advice = advice;
	}
}
